package alpaca.core;

import java.util.Date;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;


/**
 * This class is the implementation of IMessage used. It has annotations
 * and fields specifically for the ORMLite implementation.
 *
 */
@DatabaseTable(tableName = "logs")
public class Message implements IMessage {
	
	@DatabaseField(generatedId = true)
	private int id;
	
	@DatabaseField(canBeNull = false, dataType = DataType.LONG_STRING)
	private String message;
	@DatabaseField(canBeNull = false, columnName = "fromId", foreign = true, foreignAutoRefresh = true)
	private ORMLiteUser fromUser;
	@DatabaseField(canBeNull = false, columnName = "toId", foreign = true, foreignAutoRefresh = true)
	private ORMLiteUser toUser;
	@DatabaseField(canBeNull = false)
	private Date date;
	@DatabaseField(canBeNull = false)
	private boolean sent;

	private boolean messageByMe;
	
	@Deprecated
	public Message() {
		
	}
	
	public Message(String message, IUser activeUser, IUser toUser, Date date) {
		if(message == null || activeUser == null || toUser == null) {
			throw new IllegalArgumentException();
		}

		this.message = message;
		this.fromUser = (ORMLiteUser) activeUser;
		this.toUser = (ORMLiteUser) toUser;
		this.date = (date != null) ? new Date(date.getTime()) : null;
		
	}
	
	@Override
	public Date getDate() {
		return (date != null) ? new Date(date.getTime()) : null;
	}
	
	@Override
	public String getMessage() {
		return message;
	}

	@Override
	public ORMLiteUser getFromUser() {
		return fromUser;
	}

	@Override
	public ORMLiteUser getToUser() {
		return toUser;
	}
	
	@Override
	public void setMessageByMe(boolean messageByMe) {
		this.messageByMe = messageByMe;
	}
	
	@Override
	public boolean isMessageByMe() {
		return messageByMe;
	}
	
	@Override
	public boolean isSent() {
		return sent;
	}

	@Override
	public void setSent(boolean sent) {
		this.sent = sent;
	}

}

